#!/usr/bin/python3
# -*- coding: utf-8 -*-

import re

import allure
from playwright.sync_api import Page

from models.base_page import BasePage


class HomePage(BasePage):
    def __init__(self, page: Page):
        super().__init__(page)

    def check_home_page_top_navigation_bar_element_to_be_visible(self):
        """检查顶部导航栏的元素是否可见"""
        with allure.step("检查HomePage:顶部导航栏的元素是否可见"):
            self.expect.to_be_visible(self.page.locator("#gh-logo"))  # 左上角公司logo
            self.expect.to_be_visible(self.page.get_by_role("link", name="工作台"))
            self.expect.to_be_visible(self.page.get_by_role("button", name="产品与服务"))
            self.expect.to_be_visible(self.page.get_by_role("button", name="项目"))
            self.expect.to_be_visible(self.page.get_by_placeholder("搜索产品/服务/资源"))
            self.expect.to_be_visible(self.page.get_by_role("link", name="资源", exact=True))
            self.expect.to_be_visible(self.page.get_by_role("link", name="费用"))
            self.expect.to_be_visible(self.page.get_by_role("link", name="工单", exact=True))
            self.expect.to_be_visible(self.page.locator(".gh-notify-link"))

    def check_home_page_my_favorite_element_to_be_visible(self):
        """检查我的收藏元素是否可见"""
        with allure.step("检查HomePage:顶部导航栏元素是否可见"):
            self.expect.to_be_visible(self.page.get_by_text("我的收藏"))

    def check_home_page_resource_usage_ranking_element_to_be_visible(self):
        """检查主页使用排行元素是否可见"""
        with allure.step("检查HomePage:使用排行元素是否可见"):
            self.expect.to_be_visible(self.page.get_by_text("资源使用排行"))
            self.expect.to_be_visible(self.page.get_by_text("进", exact=True))
            self.expect.to_be_visible(self.page.get_by_text("出", exact=True))

    def check_home_page_recently_used_items_element_to_be_visible(self):
        """检查最近使用的项目元素是否可见"""
        with allure.step("检查HomePage:最近使用的项目元素是否可见"):
            self.expect.to_be_visible(self.page.get_by_text("最近使用的项目"))
            self.expect.to_be_visible(self.page.get_by_text("创建新的项目"))

    def check_home_page_my_service_element_to_be_visible(self):
        """检查我的服务元素是否可见"""
        with allure.step("检查HomePage:我的服务元素是否可见"):
            self.expect.to_be_visible(self.page.get_by_text("我的服务"))
            self.expect.to_be_visible(self.page.get_by_text("全局", exact=True))

    def check_home_page_account_element_to_be_visible(self):
        """检查账户模块元素是否可见"""
        with allure.step("检查HomePage:账户模块元素是否可见"):
            self.expect.to_be_visible(self.page.locator(".user-avatar"))
            self.expect.to_be_visible(self.page.get_by_text("账户可用余额"))
            self.expect.to_be_visible(self.page.get_by_role("link", name="优惠券余额:"))
            self.expect.to_be_visible(self.page.get_by_role("link", name="立即充值"))
            self.expect.to_be_visible(self.page.get_by_role("link", name="账户设置 "))

    def check_home_page_alert_element_to_be_visible(self):
        """检查告警模块元素是否可见"""
        with allure.step("检查HomePage:告警模块元素是否可见"):
            self.expect.to_be_visible(self.page.get_by_text("指标告警"))
            self.expect.to_be_visible(self.page.get_by_text("告警中"))
            self.expect.to_be_visible(self.page.get_by_text("未知"))
            self.expect.to_be_visible(self.page.get_by_text("近7天告警总数"))
            self.expect.to_be_visible(self.page.get_by_text("近24小时事件告警"))
            self.expect.to_be_visible(self.page.get_by_text("异常事件"))
            self.expect.to_be_visible(self.page.locator("span").filter(has_text="状态变更"))

    def check_home_page_help_consult_feedback_element_to_be_visible(self):
        """检查右下角的帮助、咨询、反馈元素是否可见"""
        with allure.step("检查HomePage:帮助、咨询、反馈元素是否可见"):
            self.expect.to_be_visible(self.page.locator("li").filter(has_text="帮助"))
            self.expect.to_be_visible(self.page.locator("li").filter(has_text="咨询"))
            self.expect.to_be_visible(self.page.locator("li").filter(has_text="反馈"))

    def check_home_page_message_center_element_to_be_visible(self):
        """检查消息中心元素是否可见"""
        with allure.step("检查HomePage:消息中心元素是否可见"):
            self.expect.to_be_visible(self.page.get_by_text("AppCenter"))
            self.expect.to_be_visible(self.page.get_by_text("产品消息"))
            self.expect.to_be_visible(self.page.get_by_text("财务消息"))
            self.expect.to_be_visible(self.page.get_by_text("服务消息"))

    def check_hoem_page_banner_inner_element_to_be_visible(self):
        """检查活动横幅元素是否可见"""
        with allure.step("检查HomePage:活动横幅元素是否可见"):
            self.expect.to_be_visible(self.page.locator(".banner-inner"))
